package com.servlet.adminaction;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.database.connection.DatabaseConnection;
import com.entity.User;

@WebServlet("/ListAllUser")
public class UserListing extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		String sql = "select * from user";
		try {
			conn = DatabaseConnection.getConnection();
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			ArrayList<User> users = new ArrayList<User>();
			while(rs.next()) {
				String userName = rs.getString("UserName");
				String password = rs.getString("Password");
				String type = rs.getString("Type");
				Date registeredDate = rs.getDate("RegisteredDate");
				User user = new User(userName, password, type, registeredDate);
				users.add(user);
			}
			req.setAttribute("userList", users);
			req.getRequestDispatcher("ListAllUser.jsp").forward(req, resp);
		} catch (SQLException se) {
			se.printStackTrace();
		}
	}
}
